// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const uniIdCommon = require('uni-id-common');
const { setting, settingUpdate, settingReset } = require('./module/setting/index');
const { getUserInfo, checkVisitor, changeChapter, unbindMobile } = require('./module/user/index');
const { customChapterCreate, customChapterEdit, customChapterRemove, customCourseCreate, customCourseSort, setActive, complete } = require('./module/course/index');
const { createCharacter, deleteCharacter } = require('./module/character/index');
const { report, capacity, keyboard } = require('./module/stats/index');
const { groupList, groupCreate, groupChange, groupMembers, groupMemberDelete, groupMemberExit, groupExitApply, groupChapterChange, groupChpaterList, groupRemove, groupApplyCreate, groupApplyChange } = require('./module/group/index');
const demo = require('./module/demo/index');
const { chapterList, userChapterList, categoryCourseList, userChapterComplateList, courseCompleteList, userCustomChapterList } = require('./module/list/index');
const db = uniCloud.database();
module.exports = {
	_before: async function() { // 通用预处理器
		// console.log(this.getClientInfo())
		const methodName = this.getMethodName()
		if (methodName != 'checkVisitor' && !this.getUniIdToken()) {
			throw new Error('非法访问')
		}
		this.uniIdCommon = uniIdCommon.createInstance({
			clientInfo: this.getClientInfo()
		})
	},
	async checkLogin() {
		const token = this.getUniIdToken();
		if (token) {
			const checkTokenRes = await this.uniIdCommon.checkToken(token, {
				autoRefresh: true
			})
			const { errCode, errMsg } = checkTokenRes;
			if (errCode) {
				return {
					errCode,
					errMsg
				}
			}
		}
	},
	setting,
	settingUpdate,
	settingReset,
	// 解绑手机
	unbindMobile,
	// 检查游客信息
	checkVisitor,
	getUserInfo,
	// 创建角色
	createCharacter,
	// 删除角色
	deleteCharacter,
	setActive,
	complete,
	chapterList,
	userChapterList,
	userCustomChapterList,
	categoryCourseList,
	userChapterComplateList,
	courseCompleteList,
	groupChpaterList,
	changeChapter,
	report,
	capacity,
	keyboard,
	groupList,
	groupCreate,
	groupChange,
	groupMembers,
	groupMemberDelete,
	groupMemberExit,
	groupExitApply,
	groupChapterChange,
	groupRemove,
	groupApplyCreate,
	groupApplyChange,
	customChapterCreate,
	customChapterEdit,
	customChapterRemove,
	customCourseCreate,
	customCourseSort,
	demo,
	async test() {
		const { data } = await db.collection('a-dz-chapter-list').get();
		return {
			code: 0,
			data,
			msg: '获取成功'
		}
	}
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	/* 
	method1(param1) {
		// 参数校验，如无参数则不需要
		if (!param1) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		// 业务逻辑
		
		// 返回结果
		return {
			param1 //请根据实际需要返回值
		}
	}
	*/
}